package com.ifp.core.connector;

import java.util.HashMap;
import java.util.Map;

import com.ifp.core.exception.BaseException;
import com.ifp.gateway.connector.connect.HttpConnect;
import com.ifp.gateway.connector.connect.IConnect;
import com.ifp.gateway.formatter.IFormatter;
import com.ifp.opengate.boot.emum.ErrorEnum;

/**
 * @className: HTTPIFP2ServiceConnector
 * @description: IFPHTTP连接器
 * @author: 码农7号
 * @email: guanhh1@belink.com
 * @create: 2019-07-10 09:28
 **/
public class HTTPIFP2ServiceConnector extends AbstractConnector<Object, Object> {

    @Override
    public Object sendAndReceive(String actionName, String path, Object object) {
        HttpConnect httpConnect = (HttpConnect)getIdleConnect(path);

        try {
            httpConnect.setUrl(path);
            httpConnect.setEncoding("UTF-8");
            Map<String, String> map = new HashMap<String, String>();
            // Map<String,String> headerMap=(Map<String, String>) object.get("header");
            map.putAll((Map<String, String>)object);
            httpConnect.setHeadMap(map);
            return httpConnect.sendAndReceive(object);
        } catch (Exception e) {
            throw new BaseException(ErrorEnum.connectError.code(), ErrorEnum.connectError.msg());
        } finally {
            // 释放连接
            this.releaseConnect(httpConnect);
        }
    }

    @Override
    public void initConnector() {

    }

    @Override
    public String getIp() {
        return super.getIp();
    }

    @Override
    public void setIp(String ip) {
        super.setIp(ip);
    }

    @Override
    public String getPort() {
        return super.getPort();
    }

    @Override
    public void setPort(String port) {
        super.setPort(port);
    }

    @Override
    public String getUsname() {
        return super.getUsname();
    }

    @Override
    public void setUsname(String usname) {
        super.setUsname(usname);
    }

    @Override
    public String getPasswd() {
        return super.getPasswd();
    }

    @Override
    public void setPasswd(String passwd) {
        super.setPasswd(passwd);
    }

    @Override
    public IFormatter getRequestFormatter() {
        return super.getRequestFormatter();
    }

    @Override
    public void setRequestFormatter(IFormatter requestFormatter) {
        super.setRequestFormatter(requestFormatter);
    }

    @Override
    public IFormatter getResponseFormatter() {
        return super.getResponseFormatter();
    }

    @Override
    public void setResponseFormatter(IFormatter responseFormatter) {
        super.setResponseFormatter(responseFormatter);
    }

    /**
     * 取得连接
     * 
     * @param path
     * @return
     */
    public IConnect getIdleConnect(String path) {
        HttpConnect connect = new HttpConnect();
        connect.setUrl(path);
        connect.setProxyPwd(getPasswd());
        connect.setProxyPort(Integer.parseInt(getPort()));
        connect.setProxyUser(getUsname());
        connect.setEncoding("UTF-8");
        connect.setURLEncoderEncoding("UTF-8");
        connect.setConnectTimeOut(6000);
        connect.setReadTimeOut(60000);
        connect.setMethod("POST");
        connect.setInUse(true);
        connect.setContentType("application/json;charset=utf8");

        return connect;
    }

    /**
     * 释放
     */
    protected synchronized void releaseConnect(IConnect conn) {
        conn.setInUse(false);
        notifyAll();
    }

}
